define(function(){

	var CloudFileInput=function(initConfig){
		var cfInput=this;
		initConfig=initConfig || {};
		cfInput.candidateType=initConfig.candidateType || 'folder';
		cfInput.selectorMode=initConfig.selectorMode || 'none'
		cfInput.defaultContext='选择课程资源';
		cfInput.config={
			disabled:false,
			className:$.getDefined(initConfig.className,'btn-primary')
		}
		cfInput.$dom=$('<div class="btn fileSelectorInput" ></div>');
		cfInput.$icon=$('<span class="icon icon-size-16 icon-btn-plus"></span>').appendTo(cfInput.$dom);
		cfInput.$context=$('<span class="context">'+cfInput.defaultContext+'</span>').appendTo(cfInput.$dom);
		cfInput.callback=initConfig.callback ||{};
		cfInput.callback.done=cfInput.callback.done||function(){};

		cfInput.$dom.addClass(cfInput.config.className);

		cfInput.changeFolderName=function(newFolderName){
			var pathIndex=newFolderName.lastIndexOf('/');
			if(pathIndex>=0){
				newFolderName=newFolderName.substr(pathIndex+1);
			}
			cfInput.$dom.attr({
				'title':newFolderName
			});
			cfInput.$context.text(newFolderName);
		}
		cfInput.change=function(newFileData){
			cfInput.changeFolderName(newFileData.pathStr);
			cfInput.$dom.attr({
				'data-value':newFileData.id
			}).addClass('selected');
			cfInput.$icon.attr('className','icon icon-size-16 icon-btn-folder');
			cfInput.data= $.extend(true, {}, newFileData);
		}
		cfInput.setValue=function(folderId,folderName){
			function showChangeDone(){
				var newFolderData={
					id:folderId,
					pathStr:folderName
				};
				cfInput.change(newFolderData);
				cfInput.callback.done(newFolderData);
			}
			if(folderName!==undefined){
				showChangeDone();
			}else{
				getCloudDiskFolderInfo(function(folderInfoData){
					folderName=folderInfoData.pathStr;
					showChangeDone();
				},folderId);
			}
		}
		cfInput.disabled=function(status){
			if(status===undefined){
				return cfInput.config.disabled;
			}else{
				if(status===true){
					cfInput.config.disabled=true;
					cfInput.$dom.addClass('disabled');
				}else{
					cfInput.config.disabled=false;
					cfInput.$dom.removeClass('disabled');
				}
			}

		}


		switch(cfInput.selectorMode){
			case 'normal':
				cfInput.cloudDiskSelector=new CloudDiskSelector();
				cfInput.cloudDiskSelector.popUpBox.callback.yesAfter=function(){
					cfInput.setValue(cfInput.cloudDiskSelector.checkedFolderData.id);
				}
				var cloudDiskPrevLoad=false;
				cfInput.$dom.on('mouseenter',function(){
					if(cfInput.config.disabled===true){
						return false;
					}

				})
				cfInput.$dom.on('click',function(){
					if(cfInput.config.disabled===true){
						return false;
					}

					if(cloudDiskPrevLoad===false){
						cloudDiskPrevLoad=true;
						cfInput.cloudDiskSelector.rootFolderBox.folderInitById(0,function(){
							cfInput.cloudDiskSelector.rootFolderBox.open();
							cfInput.cloudDiskSelector.popUpBox.show();
						});
					} else {
						cfInput.cloudDiskSelector.popUpBox.show();
					}

				});
				break;
			case 'renew':
				cfInput.$dom.on('click',function(){
					if(cfInput.config.disabled===true){
						return false;
					}
					cfInput.cloudDiskSelector=new CloudDiskSelector();
					cfInput.cloudDiskSelector.rootFolderBox.folderInitById(0,function(){
						cfInput.cloudDiskSelector.rootFolderBox.open();
					});
					cfInput.cloudDiskSelector.popUpBox.callback.yesAfter=function(){
						cfInput.setValue(cfInput.cloudDiskSelector.checkedFolderData.id);
					}
					cfInput.cloudDiskSelector.popUpBox.show();
				});
		}
	}

	window.CloudFileInput=CloudFileInput;
	return CloudFileInput;
})
